<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property string $id
 * @property string $code
 * @property string $username
 * @property string $email
 * @property string $password
 * @property string $first_name
 * @property string $last_name
 * @property string $photo
 * @property string $dob
 * @property string $address_id
 * @property string $gender_id
 * @property string $religion_id
 * @property string $nationality
 * @property string $national_id
 * @property string $passport_no
 * @property string $married_status
 * @property string $introducer_id
 * @property string $acknowledge
 * @property string $status
 * @property string $role_id
 * @property string $create_by
 * @property string $create_date
 * @property string $update_by
 * @property string $update_date
 */
class User extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return User the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('code, password, email, first_name, last_name, dob, introducer_id, acknowledge', 'required'),
            array('code, username, email, password, first_name, last_name, photo, nationality, national_id, passport_no', 'length', 'max' => 255),
            array('gender_id, religion_id, introducer_id, role_id, create_by, update_by', 'length', 'max' => 20),
            array('married_status, acknowledge, status', 'length', 'max' => 3),
            array('dob, create_date, update_date', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, code, username, email, password, first_name, last_name, photo, dob, gender_id, religion_id, nationality, national_id, passport_no, married_status, introducer_id, acknowledge, status, role_id, create_by, create_date, update_by, update_date', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'address' => array(self::HAS_ONE, 'Address', 'user_id'),
            'bank_account' => array(self::HAS_ONE, 'UserBankAccount', 'user_id'),
            'beneficiary' => array(self::HAS_ONE, 'Beneficiary', 'user_id')
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'code' => 'Code',
            'username' => 'Username',
            'email' => 'Email',
            'password' => 'Password',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'photo' => 'Photo',
            'dob' => 'Date of Birth',
            'gender_id' => 'Gender',
            'religion_id' => 'Religion',
            'nationality' => 'Nationality',
            'national_id' => 'National ID',
            'passport_no' => 'Passport No',
            'married_status' => 'Married Status',
            'introducer_id' => 'Introducer',
            'acknowledge' => 'Acknowledge',
            'status' => 'Status',
            'role_id' => 'Role',
            'create_by' => 'Create By',
            'create_date' => 'Create Date',
            'update_by' => 'Update By',
            'update_date' => 'Update Date',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('code', $this->code, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('photo', $this->photo, true);
        $criteria->compare('dob', $this->dob, true);
        $criteria->compare('gender_id', $this->gender_id, true);
        $criteria->compare('religion_id', $this->religion_id, true);
        $criteria->compare('nationality', $this->nationality, true);
        $criteria->compare('national_id', $this->national_id, true);
        $criteria->compare('passport_no', $this->passport_no, true);
        $criteria->compare('married_status', $this->married_status, true);
        $criteria->compare('introducer_id', $this->introducer_id, true);
        $criteria->compare('acknowledge', $this->acknowledge, true);
        $criteria->compare('status', $this->status, true);
        $criteria->compare('role_id', $this->role_id, true);
        $criteria->compare('create_by', $this->create_by, true);
        $criteria->compare('create_date', $this->create_date, true);
        $criteria->compare('update_by', $this->update_by, true);
        $criteria->compare('update_date', $this->update_date, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    public function getFullName(){
        return $this->first_name . ' ' . $this->last_name;
    }
    
    public function getUpLinks() {
        $maxUpperLevel = AppDefinedValues::model()->find("category = :MAX_LEVEL", array(':MAX_LEVEL' => 'MAX_LEVEL'));
        $nextUpLinkId = $this->introducer_id;
        for ($i = 1; $i <= $maxUpperLevel->value; $i++) {
            if($nextUpLinkId != null) {
            $user = User::model()->find("introducer_id = :NEXT_SPONSOR_ID", array(':NEXT_SPONSOR_ID' => $nextUpLinkId));
            $user->bank_account = UserBankAccount::model()->find("user_id = :USR_ID", array(':USR_ID' => $user->id));
            $upLinks[$i] = $user;
            $nextUpLinkId = $user->introducer_id;
            } else {
                break;
            }
        }
        return $upLinks;
    }
}